<%@ page import="java.time.LocalDateTime" %><%--
  Created by IntelliJ IDEA.
  User: ADMIN
  Date: 2023/3/27
  Time: 21:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%
    request.setCharacterEncoding("utf-8");
    //判断需要处理的逻辑//
    //       state = 1  插入 2/修改 3 删除 8 取消收藏

    Integer userid = Integer.valueOf(session.getAttribute("id").toString());
    Integer depart = Integer.valueOf(session.getAttribute("depart").toString());
    Integer state = Integer.valueOf(request.getParameter("state"));
    String opereation = null;
    Integer carcircleid = null;



    Integer id = null;
    String username = null;
    String createtime = null;
    String avatar = null;
    String message = null;
    ;

    if(state == 1) {
        carcircleid= Integer.valueOf(request.getParameter("carcircleid").toString());
        username = session.getAttribute("username").toString();;
        avatar = session.getAttribute("avatar").toString();
        message = request.getParameter("message");
        createtime = LocalDateTime.now().toString();
        userid = Integer.valueOf(session.getAttribute("id").toString());

    }else {
        id = Integer.valueOf(request.getParameter("id"));
    }

    String sql = null;
    if(state == 1){
        //新增
        opereation = "新增评论";
        sql = "INSERT INTO comment(username,message,avatar,userid,createtime,carcircleid) VALUES ( '"+username+"', '"+message+"', '"+avatar+"', "+userid+", '"+createtime+"',"+carcircleid+")";
    }else {
        opereation = "删除评论";
        //删除
        sql = "delete from liuyan where id = "+id;
    }

    System.out.println(sql);
    //jdbc连接数据库

    //jdbc 操作
    String url = "jdbc:mysql://localhost:3306/carxianyu";
    String user = "root";
    String pwd = "root";
    int rs = 0;
    Connection conn=null;
    try {
        Class.forName("com.mysql.jdbc.Driver");//加载驱动
        conn = DriverManager.getConnection("jdbc:mysql://43.142.239.28:13307/carxianyu?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC","root","root");
        PreparedStatement a = conn.prepareStatement(sql);
        rs = a.executeUpdate(sql);
        System.out.println("执行了sql =====》"+sql);


        if (rs != 0 && state ==1){
            out.println("<script type='text/javascript'>alert('"+opereation+"成功');window.location.href='/view/front/carCircle/carcircleComment.jsp?id="+carcircleid+"';</script>");

        }


    }catch (Exception exception){}finally {
        conn.close();
    }


%>



<head>
    <title>处理cru的逻辑</title>
</head>

</html>
